﻿ #!/usr/bin/env python
'''
@author: bastien
'''

from src.model.Game import *
from src.model.Player import *

import json

from google.appengine.ext import webapp


class GameHandler(webapp.RequestHandler):
    games = {}
    players = {}

    def get(self):
        args = self.request.arguments()

        if("req" in args):   # on fait une requete
            req_type = self.request.get("req")
            gid = self.request.get("id")

            if req_type == "get_current_player":   # renvoyer le joueur a qui c'est le tour
                self.response.out.write(json.dumps({"name": self.games[gid].getCurrentPlayer()}))
            elif req_type == "get_cell":   # renvoie la cellule (game_id, x, y) return (cell_x, cell_y, cell_letter, is_active)
                x = int(self.request.get("x"))
                y = int(self.request.get("y"))
                table = self.games[gid].get_table()
                letter = table[x][y].get_letter()
                isActivated = table[x][y].isActivated
                self.response.out.write(json.dumps({"letter": letter, "isActivated": isActivated}))
            elif req_type == "get_player":   # renvoie les infos sur le joueur (player_name, score)
                player_name = self.request.get("player_name")
                player_score = self.players[player_name].get_score()
                self.response.out.write(json.dumps({"name": player_name, "score": player_score}))
            elif req_type == "get_game_started":   # true si la partie est commencee
                if(self.games[gid].getCurrentPlayer() == ""):
                    self.response.out.write(json.dumps({"gameStarted": False}))
                else:
                    self.response.out.write(json.dumps({"gameStarted": True}))
            elif req_type == "get_game_finished":  # true si la partie est terminée
                self.response.out.write(json.dumps({"gameFinished": self.games[gid].isFinished()}))
            elif req_type == "get_players":  # renvoie les noms des joueurs sur cette partie
                players = self.games[gid].players
                self.response.out.write(json.dumps({"player1": players[0].get_name(), "player2": players[1].get_name()}))
            elif req_type == "is_valid_word":
                word = self.request.get("word")
                self.response.out.write(json.dumps({"word_valid": Dict().validWord(word)}))
        else:
            self.response.out.write("Wrong request type<br />\
                Usage: ?req=req_type<br />")

            for arg in args:
                self.response.out.write(arg + ": " + self.request.get(arg) + "<br />")

    def post(self):
        args = self.request.arguments()

        if("req" in args):  # on fait une requete
            req_type = self.request.get("req")
            gid = self.request.get("id")

            if req_type == "play_word":  # jouer un mot (game_id, player_id, word, x, y, letter)
                player_name = self.request.get("player_name")
                letter = self.request.get("letter")
                word = self.request.get("word")
                x = self.request.get("x")
                y = self.request.get("y")
                self.games[gid].play(
                    word,
                    x,
                    y,
                    letter)
                self.response.out.write(player_name + " played letter " + letter + " in (" + x + ", " + y + ") to make word " + word)
                self.response.out.write("new player is now " + self.games[gid].getCurrentPlayer())
            elif req_type == "new_game":  # creer un nouveau tableau de jeu (game_id)  # plus tard: size-x, size-y
                if(not gid in self.games):
                    self.games[gid] = Game()
                    self.response.out.write("new game added " + gid)
            elif req_type == "add_player":   # ajoute un nouveau joueur a la partie (game_id, player_name)
                player_name = self.request.get("player_name")
                playerToAdd = Player(player_name)
                self.players[player_name] = playerToAdd
                self.games[gid].addPlayer(playerToAdd)
                self.response.out.write("player " + player_name + " added")
            elif req_type == "skip_player":  # le joueur passe son tour
                self.games[gid].SkipPlayer()

        else:
            self.response.out.write("Wrong request type<br />\
                Usage: ?req=req_type<br />")
            for arg in args:
                self.response.out.write(arg + ": " + self.request.get(arg) + "<br />")
